import random
import time
import curses
from curses.textpad import Textbox, rectangle

MIN = 0
MAX = 16
card_w = 3
card_h = 2

target = random.randint(MIN, MAX)
guess_time = 0

RED = 1
BLUE = 2
GREEN = 3


def basic_logic():
    """
    - just for reference
    """
    # print(f"The answer is {target}")
    print(f'Guess a number from {MIN} to {MAX}. Press \'q\' to quit the game')
    ip = input('Please guess:')
    while True:
        guess_time += 1
        if int(ip) > target:
            ip = input('Big! Guess again:')
        elif int(ip) < target:
            ip = input('Small! Guess again:')
        else:
            break


def TUI_init():
    screen = curses.initscr()
    curses.cbreak()
    screen.keypad(True)
    curses.start_color()
    curses.init_pair(RED, curses.COLOR_RED, curses.COLOR_BLACK)
    curses.init_pair(BLUE, curses.COLOR_BLUE, curses.COLOR_BLACK)
    curses.init_pair(GREEN, curses.COLOR_GREEN, curses.COLOR_BLACK)
    global max_y, max_x
    max_y, max_x = screen.getmaxyx()
    return screen

def TUI_free(srceen):
    curses.nocbreak()
    srceen.keypad(False)
    curses.echo()
    curses.endwin()

def TUI_input(screen, prompt):
    screen.addstr(0, 0, '                                 ')
    screen.addstr(0, 0, prompt)
    ret = screen.getstr()
    return ret


class Card():
    def __init__(self, screen, num, y, x, h, w):
        self.y = y
        self.x = x
        self.h = h
        self.w = w
        self.screen = screen
        self.num = str(num)
        self.numy = y + 1
        self.numx = x + 1
        rectangle(screen, self.y, self.x, self.y + h, self.x + w)
        screen.addstr(self.numy, self.numx, self.num)
    
    def change_color(self, pair_no):
        self.screen.addstr(self.numy, self.numx, self.num, curses.color_pair(pair_no))


def get_cards(screen, min, max):
    cards = []
    for i in range(min, max + 1):
        # draw_card(stdscr, i, (card_h) * i, 2, card_h, card_w)
        card = Card(screen, i, 2, (card_w + 1) * i, card_h, card_w)
        cards.append(card)
    return cards


def main():
    stdscr = TUI_init()
    before_time = time.time()
# ========================================

    cards = get_cards(stdscr, MIN, MAX)
    ip = TUI_input(stdscr, "Please guess:")
    while True:
        if ip.isdigit(): ip = int(ip)
        else: 
            ip = TUI_input(stdscr, f'Please input a number in [{MIN},{MAX}]:')
            continue
        if ip > MAX or ip < MIN:
            ip = TUI_input(stdscr, f'Please input a number in [{MIN},{MAX}]:')
        elif ip > target:
            cards[ip - MIN].change_color(RED)
            ip = TUI_input(stdscr, 'Big! Guess again:')
        elif ip < target:
            cards[ip - MIN].change_color(BLUE)
            ip = TUI_input(stdscr, 'Small! Guess again:')
        else:
            cards[ip - MIN].change_color(GREEN)
            break

# ========================================
    after_time = time.time()
    stdscr.addstr(6, 0, f'Congratulation! You have already wasted {int(after_time - before_time)} seconds in your life')
    stdscr.getch()
    TUI_free(stdscr)

main()
